package top.callname.service.impl;

import org.apache.log4j.Logger;

import top.callname.bean.ConstanstBean;
import top.callname.bean.RspInfoBean;
import top.callname.bean.StudentBean;
import top.callname.bean.StudentPageList;
import top.callname.dao.StudentDao;
import top.callname.service.StudentService;
import top.callname.util.BaseUtil;

public class StudentServiceImpl implements StudentService{

	private StudentDao studentDao = new StudentDao();
	Logger logger = Logger.getLogger(StudentServiceImpl.class);
	
	@Override
	public StudentPageList getListPage(int page, Integer cid) {
		return studentDao.selectPageByCid(page, cid);
	}

	@Override
	public StudentBean getByStuNo(String stuNo) {
		return studentDao.selectByStuNo(stuNo);
	}

	@Override
	public RspInfoBean bindWeixin(String stuNo, String openid) {
		RspInfoBean rsp = new RspInfoBean();
		//查询是否存在学号
		StudentBean bean = studentDao.selectByStuNo(stuNo);
		try {
			if(bean==null){
				rsp.setRspDesc("该学号["+stuNo+"]不存在系统中，请检查是否正确！");
				return rsp;
			}else{
				//如果openid为0 表示未绑定该用户，可以进行绑定，否则提示已绑定
				if(ConstanstBean.NOT_BIND.equals(bean.getOpenid())){
					if(studentDao.selectByOpenid(openid)==null){
						if(studentDao.updateOpenid(stuNo, openid)){
							rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
							rsp.setRspDesc("绑定成功 ： 学号["+stuNo+"] !");
						}else{
							rsp.setRspDesc("绑定失败 : 学号["+stuNo+"] ！");
						}
					}else{
						rsp.setRspDesc("您的微信号已绑定过其他学号，请联系管理员解绑定！");
					}
				
				}else{
					rsp.setRspDesc("该学号["+stuNo+"]的用户已绑定，无需重复操作！");
				}
				return rsp;
			}
		}catch(Exception e){
			logger.error("绑定失败 :"+e.getMessage());
			rsp.setRspDesc("绑定失败 : 请不要重复绑定 ！");
		}
		return rsp;
	}

	@Override
	public RspInfoBean solutionBinding(Integer sid) {
		boolean f = studentDao.updateOpenid(sid);
		RspInfoBean rsp = new RspInfoBean();
		if(!f){
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("解除绑定失败 ！");
		}else{
			rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
			rsp.setRspDesc("解除绑定成功 ！");
		}
		return rsp;
	}

	@Override
	public RspInfoBean save(StudentBean student) {
		RspInfoBean rsp = new RspInfoBean();
		if(student.getStuNo()==null || "".equals(student.getStuNo().trim())){
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("学号不得为空！");
			return rsp;
		}
		if(!BaseUtil.isNumber(student.getStuNo())){
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("学号只能为数字，不得为字符串！");
			return rsp;
		}
		if(student.getStuName()==null || "".equals(student.getStuName().trim())){
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("姓名不得为空！");
			return rsp;
		}
		if(studentDao.selectByStuNo(student.getStuNo())!=null){
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("该学号已存在！");
			return rsp;
		}
		boolean f = studentDao.insert(student);
		if(!f){
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("添加失败 ！");
		}else{
			rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
			rsp.setRspDesc("添加成功 ！");
		}
		return rsp;
	}

	@Override
	public StudentBean getByOpenid(String openid) {
		StudentBean bean = studentDao.selectByOpenid(openid);
		if(bean!=null){
			bean.setRspCode(ConstanstBean.RSP_SUCCESS);
		}else{
			bean = new StudentBean();
			bean.setRspCode(ConstanstBean.RSP_FAULT);
			bean.setRspDesc("未查询到该用户！");
		}
		return bean;
	}

	@Override
	public RspInfoBean deleteBySid(Integer sid) {
		boolean f = studentDao.deleteBySid(sid);
		RspInfoBean rsp = new RspInfoBean();
		if(!f){
			rsp.setRspCode(ConstanstBean.RSP_FAULT);
			rsp.setRspDesc("删除失败 ！");
		}else{
			rsp.setRspCode(ConstanstBean.RSP_SUCCESS);
			rsp.setRspDesc("删除成功 ！");
		}
		return rsp;
	}

}
